<?php

namespace app\admin\model;

use app\common\model\UserCircle;
use app\common\model\Report;

use think\Model;


class User extends Model
{

    

    

    // 表名
    protected $name = 'user';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'add_time_text',
        'edit_time_text',
        'authen_text',
        'add_authen_time_text',
        'authen_time_text',
        'authen_type_text',
        'end_time_text',
        'is_chao_group_text',
        'is_chao_kf_text',
        'logout_text',
        'add_log_time_text',
        'authen_log_time_text',
        'is_jin_text',
        'logintime_text'
    ];
    

    
    public function getAuthenList()
    {
        return ['1' => __('Authen 1'), '2' => __('Authen 2'), '3' => __('Authen 3'), '4' => __('Authen 4')];
    }

    public function getAuthenTypeList()
    {
        return ['1' => __('Authen_type 1'), '2' => __('Authen_type 2')];
    }

    public function getIsChaoGroupList()
    {
        return ['1' => __('Is_chao_group 1'), '0' => __('Is_chao_group 0')];
    }

    // protected static function init()
    // {
    //     self::beforeUpdate(function ($row) {
    //         $changed = $row->getChangedData();
    //         //如果有修改密码
    //         if (isset($changed['password'])) {
    //             if ($changed['password']) {
    //                 $salt = \fast\Random::alnum();
    //                 $row->password = \app\common\library\Auth::instance()->getEncryptPassword($changed['password'], $salt);
    //                 $row->salt = $salt;
    //             } else {
    //                 unset($row->password);
    //             }
    //         }
    //     });


    //     self::beforeUpdate(function ($row) {
    //         $changedata = $row->getChangedData();
    //         $origin = $row->getOriginData();
    //         if (isset($changedata['money']) && (function_exists('bccomp') ? bccomp($changedata['money'], $origin['money'], 2) !== 0 : (double)$changedata['money'] !== (double)$origin['money'])) {
    //             UserCircle::create(['user_id' => $row['id'], 'money' => $changedata['money'] - $origin['money'], 'before' => $origin['money'], 'after' => $changedata['money'], 'memo' => '管理员变更金额']);
    //         }
    //         if (isset($changedata['score']) && (int)$changedata['score'] !== (int)$origin['score']) {
    //             Report::create(['user_id' => $row['id'], 'score' => $changedata['score'] - $origin['score'], 'before' => $origin['score'], 'after' => $changedata['score'], 'memo' => '管理员变更积分']);
    //         }
    //     });
    // }
    public function getIsChaoKfList()
    {
        return ['1' => __('Is_chao_kf 1'), '0' => __('Is_chao_kf 0')];

    }

    public function getLogoutList()
    {
        return ['0' => __('Logout 0'), '1' => __('Logout 1'), '2' => __('Logout 2'), '3' => __('Logout 3')];
    }

    public function getIsJinList()
    {
        return ['0' => __('Is_jin 0'), '1' => __('Is_jin 1')];
    }


    public function getAddTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['add_time']) ? $data['add_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEditTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['edit_time']) ? $data['edit_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getAuthenTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['authen']) ? $data['authen'] : '');
        $list = $this->getAuthenList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getAddAuthenTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['add_authen_time']) ? $data['add_authen_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getAuthenTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['authen_time']) ? $data['authen_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getAuthenTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['authen_type']) ? $data['authen_type'] : '');
        $list = $this->getAuthenTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getIsChaoGroupTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_chao_group']) ? $data['is_chao_group'] : '');
        $list = $this->getIsChaoGroupList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsChaoKfTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_chao_kf']) ? $data['is_chao_kf'] : '');
        $list = $this->getIsChaoKfList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getLogoutTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['logout']) ? $data['logout'] : '');
        $list = $this->getLogoutList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getAddLogTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['add_log_time']) ? $data['add_log_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getAuthenLogTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['authen_log_time']) ? $data['authen_log_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getIsJinTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_jin']) ? $data['is_jin'] : '');
        $list = $this->getIsJinList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getLogintimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['logintime']) ? $data['logintime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setAddTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEditTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setAddAuthenTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setAuthenTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setAddLogTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setAuthenLogTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setLogintimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function industry()
    {
        return $this->belongsTo('Industry', 'industry_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function type()
    {
        return $this->belongsTo('app\admin\model\company\Type', 'company_typeid', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function position()
    {
        return $this->belongsTo('Position', 'position_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


}